import json

def write_file(msg):
    with open('calcu.txt', 'a',encoding='utf-8') as f:
        f.writelines(msg + "\n")

def load_config(config_file="config.json"):
    with open(config_file, "r") as file:
        config = json.load(file)
    return config

config = load_config()
j_file_name = config["j_file_name"] 

if j_file_name is not None and j_file_name != "" :
    with open(j_file_name, 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('交通部交易条数:',num,'笔') 
        write_file('交通部交易条数:' + num + '笔')
        hex_str = hex_str[42:]
        
        i=0
        money = 0
        while i<num:
            money = money +  int(hex_str[i*364+114:i*364+118],16)
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')

if config["y_file_name"] is not None and config["y_file_name"] != "" :
    with open(config["y_file_name"], 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('一卡通交易条数:',num,'笔') 
        write_file('一卡通交易条数:' + str(num) + '笔')
        hex_str = hex_str[42:]
        
        i=0
        money = 0
        while i<num:
            if hex_str[i*252:i*252+2] == '63':
                money = money +  int(hex_str[i*252+74:i*252+78],16)
            # print(hex_str[i*252+74:i*252+78])
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')

if config["s_file_name"] is not None and config["s_file_name"] != "" :
    with open(config["s_file_name"], 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('单程票交易条数:',num,'笔') 
        write_file('单程票交易条数:' + str(num) + '笔')
        hex_str = hex_str[42:]

        
        
        i=0
        money = 0
        while i<num:
            # print(hex_str[i*226:i*226+2])
            if hex_str[i*226:i*226+2] == '03':
                money = money +  int(hex_str[i*226+98:i*226+102],16)
            # else:
            #     print(hex_str[i*226:i*226+2],int(hex_str[i*226+98:i*226+102],16))
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')

if config["v_file_name"] is not None and config["v_file_name"] != "" :    
    with open(config["v_file_name"], 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('员工票交易条数:',num,'笔') 
        write_file('员工票交易条数:' + str(num) + '笔')
        hex_str = hex_str[42:]
        
        i=0
        money = 0
        while i<num:
            money = money +  int(hex_str[i*226+98:i*226+102],16)
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')

if config["q_file_name"] is not None and config["q_file_name"] != "" : 
    with open(config["q_file_name"], 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('二维码交易条数:',num,'笔') 
        write_file('二维码交易条数:' + str(num) + '笔')
        hex_str = hex_str[42:]
        
        i=0
        money = 0
        while i<num:
            money = money +  int(hex_str[i*226+98:i*226+102],16)
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')

if config["p_file_name"] is not None and config["p_file_name"] != "" : 
    with open(config["p_file_name"], 'rb') as file:
        content = file.read()
        hex_str = content.hex()
        num = int(hex_str[34:42],16)
        print('银联卡交易条数:',num,'笔') 
        write_file('银联卡交易条数:' + str(num) + '笔')
        hex_str = hex_str[42:]
        
        i=0
        money = 0
        while i<num:
            money = money +  int(hex_str[i*226+98:i*226+102],16)
            i = i+1
        print('交易金额:',money/100,'元')
        write_file('交易金额:' + str(money/100) + '元')
